package book.crypto;

import java.io.DataOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

import javax.crypto.Cipher;
import javax.crypto.CipherOutputStream;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;

/**
 * 解密文件中的加密数据
 */
public class CipherOutputStreamTest {

	public static void main(String[] args) throws IOException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException {
		
		KeyGenerator gen = KeyGenerator.getInstance("des");
		SecretKey key = gen.generateKey();
		Cipher cipher = Cipher.getInstance("DES");
		
		cipher.init(Cipher.ENCRYPT_MODE, key);
		CipherOutputStream out = new CipherOutputStream(new FileOutputStream("secret.txt"), cipher);
		DataOutputStream dis = new DataOutputStream(out);
		out.write("涂有".getBytes());
		dis.close();
	}
}
